home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4910 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  68 lines

  1. Newsgroups: comp.lang.c++
  2. Path: br34.bearriver.com!dmurphy
  3. From: Duane Murphy <dmurphy@bearriver.com>
  4. Subject: Suicidal Objects (was Aborting Constructors, Part II)
  5. Sender: usenet@BearRiver.com (Usenet News)
  6. Message-ID: <DM3sAp.52H@BearRiver.com>
  7. Date: Thu, 1 Feb 1996 15:28:01 GMT
  8. X-Xxdate: Thu, 1 Feb 1996 15:26:59 GMT
  9. X-Xxmessage-Id: <AD361BC316014E22@br34.bearriver.com>
  10. Content-Transfer-Encoding: 8bit
  11. Content-Type: text/plain; charset=ISO-8859-1
  12. References: <4eoeck$t6n@news.ios.com>
  13. Nntp-Posting-Host: br34.bearriver.com
  14. Mime-Version: 1.0
  15. Organization: Bear River Associates
  16. X-Newsreader: Nuntius 2.0.4_PPC
  17.  
  18. Tilo Koerbs, mkt@isun04.inf.uni-jena.de writes:
  19. >> test::test(){
  20. >>     cout << "constructing object\n";
  21. >>     delete this;
  22. >>     throw(xmsg(string("deleted the object!\n")));
  23. >> }
  24. >
  25. >I am sure that using delete on this is not allowed in C++.
  26.  
  27. Sorry, calling delete on this is prefectly valid. 
  28.  
  29. Tilo Koerbs, mkt@isun04.inf.uni-jena.de also  writes:
  30. >After the delete the object the function works with is
  31. >not valid any more. So what is the function working with?
  32.  
  33. This part of your statement is true. The code doesnt magically go 
  34. away, but the vtable does. You cannot call any other methods 
  35. (actually only virtual methods; but its still dangerous at best)
  36.  
  37. A previous response by Sergey was more correct.
  38.  
  39. Calling delete only works if your object is heap based. There are 
  40. ways of hiding constructors such that this is gauranteed, but it is 
  41. odd behaviour. Furthermore, how is the client of the object to know 
  42. that you just called delete?
  43.  
  44. It is legal however to call the destructor as a function (I recall 
  45. reading this somewhere, please correct me.)
  46.  
  47. myclass::~myclass()
  48.  
  49. This is equally dangerous as the client of the object still has not 
  50. idea what happened.
  51.  
  52. I missed your original post so I am not sure what you are trying to 
  53. solve.
  54.  
  55. A method that I use is to place cleanup code in a seperate 
  56. function. The destructor calls this code, and the constructor can 
  57. also call this code if need be.
  58.  
  59. I am an exceptions enthusiast so my constructors will usually throw 
  60. exceptions. There are other methods of getting feedback to object 
  61. clients, but this seems fairly clean and simple.
  62.  
  63. I hope this helps,
  64. ...Duane Murphy
  65. __________________________________________________________________________
  66. Duane Murphy                                   Bear River Associates, Inc.
  67. <mailto:dmurphy@bearriver.com>                 <http://www.bearriver.com>
  68.